﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DusonFramework.Core
{
    public sealed class Compare
    {
        /// <summary>
        /// Check equal of two arrays
        /// </summary>
        /// <param name="leftArr">left array</param>
        /// <param name="rightArr">right array</param>
        /// <returns></returns>
        public static bool IsArrayEquals(IEnumerable leftArr, IEnumerable rightArr)
        {
            if ((leftArr == null && rightArr != null) ||
                (leftArr != null && rightArr == null))
            {
                return false;
            }

            IEnumerator enLeft = leftArr.GetEnumerator();
            IEnumerator enRight = rightArr.GetEnumerator();

            return IsArrayEquals(enLeft, enRight);
        }

        /// <summary>
        /// Check equal of two arrays
        /// </summary>
        /// <param name="enLeft">left array</param>
        /// <param name="enRight">right array</param>
        /// <returns></returns>
        public static bool IsArrayEquals(IEnumerator enLeft, IEnumerator enRight)
        {
            if ((enLeft == null && enRight != null) ||
                (enLeft != null && enRight == null))
            {
                return false;
            }

            bool isEquals = true;

            while (true)
            {
                bool leftHasNext = enLeft.MoveNext();
                bool rightHasNext = enRight.MoveNext();

                if ((leftHasNext && (!rightHasNext)) ||
                    ((!leftHasNext) && rightHasNext))
                {
                    isEquals = false;
                    break;
                }

                if ((!leftHasNext) && (!rightHasNext))
                {
                    break;
                }

                if ((enLeft.Current == null && enRight.Current != null) ||
                    (enLeft.Current != null && enRight.Current == null) ||
                    (!enLeft.Current.Equals(enRight.Current)))
                {
                    isEquals = false;
                    break;
                }
            }

            return isEquals;
        }

        public static bool IsEqual(object left, object right)
        {
            return IsEqual(left, right, false);
        }

        public static bool IsEqual(object left, object right, bool treatNullAndEmptyStringAsEqual)
        {
            if (left == null && right == null)
                return true;

            bool retValue = (left != null && left.Equals(right));
            if ((!retValue) && treatNullAndEmptyStringAsEqual)
            {
                if (left != null)
                    retValue = (left.ToString() == string.Empty && right == null);
                else if (right != null)
                    retValue = (right.ToString() == string.Empty && left == null);
            }
            return retValue;
        }
    }
}
